package com.maziro.spring.service.cycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

/**
 * @author songsheng
 * @since 2021/6/9
 */
@Component
public class LifeInstantiationAwareBeanPostProcessor implements SmartInstantiationAwareBeanPostProcessor {

	@Override
	@Nullable
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		if (beanName.equals("AService")) {
			System.out.println("before instant");
		}
		return null;
	}

	@Override
	public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		if (beanName.equals("AService")) {
			System.out.println("after instant");
		}
		return true;
	}

	@Override
	public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
		if (beanName.equals("AService")) {
			System.out.println("early bean");
		}
		return bean;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) {
		if (beanName.equals("AService")) {
			System.out.println("before initial");
		}
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
		if (beanName.equals("AService")) {
			System.out.println("after initial");
		}
		return bean;
	}
}
